//
//  LSExponentNode.m
//  LookScore
//
//  Created by nice on 2018/7/26.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSExponentNode.h"
#import "LSAIHistoricaModel.h"
#import "LSExponentSubNode.h"
#import "LSTypeButton.h"
@interface LSExponentNode ()<ASTableDelegate,ASTableDataSource,LSTypeButtonActionDelegate>

@property (nonatomic, strong) ASTextNode * leagueNameNode;
@property (nonatomic, strong) ASTextNode * homeTeamOrderNode;
@property (nonatomic, strong) ASTextNode * homeTeamNameNode;
@property (nonatomic, strong) ASTextNode * vsNode;
@property (nonatomic, strong) ASTextNode * guestTeamOrderNode;
@property (nonatomic, strong) ASTextNode * guestTeamNameNode;
@property (nonatomic, strong) ASTextNode * timeNode;
@property (nonatomic, strong) ASImageNode * arrowImgNode;
@property (nonatomic, strong) ASTableNode *tableNode;
//@property (nonatomic, strong) ASTableView * tableView;
@property (nonatomic, strong) ASDisplayNode * topViewNode;
@property (nonatomic, strong) ASDisplayNode * baseViewNode;
@property (nonatomic, strong) ASButtonNode * anyMoreBtn;

//@property (nonatomic, strong) UIView *topView;
//@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) LSExponentList * model;
@end

@implementation LSExponentNode{
     NSInteger typeIndex;
}

- (ASTableNode *)tableNode{
    if (!_tableNode) {
        _tableNode = [[ASTableNode alloc] initWithStyle:UITableViewStyleGrouped];
        _tableNode.delegate = self;
        _tableNode.dataSource = self;
        
    }
    return _tableNode;
}

//- (NSMutableArray *)dataArray{}

- (instancetype)initWithExpont:(LSExponentList *)model{
    if (self = [super init]) {
        _model = model;
        self.topViewNode = [[ASDisplayNode alloc] init];
       
        self.baseViewNode = [[ASDisplayNode alloc] init];
        self.baseViewNode.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.leagueNameNode = [[ASTextNode alloc] init];
        self.homeTeamOrderNode = [[ASTextNode alloc] init];
        self.homeTeamNameNode = [[ASTextNode alloc] init];
        self.guestTeamOrderNode = [[ASTextNode alloc] init];
        self.guestTeamNameNode = [[ASTextNode alloc] init];
        self.vsNode = [[ASTextNode alloc] init];
        self.timeNode = [[ASTextNode alloc] init];
        self.anyMoreBtn = [[ASButtonNode alloc] init];
        [self.anyMoreBtn setTitle:@"查看更多" withFont:LSFontSize10 withColor:LSSubGrayColorf9 forState:UIControlStateNormal];
        [self.anyMoreBtn addTarget:self action:@selector(anyMoreBtnClicked:) forControlEvents:ASControlNodeEventTouchUpInside];
        [self.anyMoreBtn setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
        
        self.leagueNameNode.attributedText = [self attributedStringForDescription:model.LeagueName withFont:LSFontSize11 WithTextColor:LSTitleWhiteColor];
        self.leagueNameNode.backgroundColor = [CommonClass colorWithHexString:model.LeagueColor];
        
        if ([CommonClass isBlankString:model.HomeTeamOrder]) {
            self.homeTeamOrderNode.attributedText = [self attributedStringForDescription:@"" withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSTitleGrayColor3];
        }else{
            NSCharacterSet* nonDigits1 = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            NSString *homeTeamOrder = [model.HomeTeamOrder stringByTrimmingCharactersInSet:nonDigits1];
            self.homeTeamOrderNode.attributedText = [self attributedStringForDescription:[NSString stringWithFormat:@"[%@]",homeTeamOrder] withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSTitleGrayColor3] ;
        }
        if ([CommonClass isBlankString:model.GuestTeamOrder]) {
            self.guestTeamOrderNode.attributedText = [self attributedStringForDescription:@"" withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSTitleGrayColor3];
        }else{
            NSCharacterSet* nonDigits1 = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
            NSString *guestTeamOrder = [model.GuestTeamOrder stringByTrimmingCharactersInSet:nonDigits1];
            self.guestTeamOrderNode.attributedText = [self attributedStringForDescription:[NSString stringWithFormat:@"[%@]",guestTeamOrder] withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSTitleGrayColor3] ;
        }
        
        self.homeTeamNameNode.attributedText = [self attributedStringForDescription:model.HomeTeamName withFont:LSFontSize(13,LSScreenWidth) WithTextColor:LSSubColor5D5D5D];
        
        self.guestTeamNameNode.attributedText = [self attributedStringForDescription:model.GuestTeamName withFont:LSFontSize(13,LSScreenWidth) WithTextColor:LSSubColor5D5D5D];
        self.timeNode.attributedText = [self attributedStringForDescription:[CommonClass timestampToString:model.TimeStamp] withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSTitleGrayColor3];
        
        
    }
    [self addSubnode:self.baseViewNode];
    [self addSubnode:self.topViewNode];
    
    [self addSubnode:self.leagueNameNode];
    [self addSubnode:self.homeTeamNameNode];
    [self addSubnode:self.homeTeamOrderNode];
    [self addSubnode:self.vsNode];
    [self addSubnode:self.guestTeamNameNode];
    [self addSubnode:self.guestTeamOrderNode];
    [self addSubnode:self.timeNode];
    [self addSubnode:self.tableNode];
    
    
    [self addSubnode:self.anyMoreBtn];
    
    if (!model.isOpen) {
        
//        self.topViewNode.hidden = YES;
        self.anyMoreBtn.hidden = YES;
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableNode reloadData];
        });
        self.anyMoreBtn.hidden = NO;
    }
    return self;
}

- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize{
    
   
    
    self.vsNode.style.width = ASDimensionMake(LSAutoVlaue(15, LSScreenWidth));
    self.vsNode.attributedText = [self attributedStringForDescription:@"VS" withFont:LSFontSize(11,LSScreenWidth) WithTextColor:LSSubColorf25e59];
    self.homeTeamOrderNode.style.width = ASDimensionMake(LSAutoVlaue(25, LSScreenWidth));
    self.homeTeamNameNode.style.width = ASDimensionMake((LSScreenWidth -200)/2);
    self.homeTeamNameNode.maximumNumberOfLines = 2;
    ASStackLayoutSpec * homeStackSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
    homeStackSpec.spacing = 3;
    homeStackSpec.justifyContent = ASStackLayoutJustifyContentStart;
    homeStackSpec.alignItems = ASStackLayoutAlignItemsCenter;
    homeStackSpec.children = @[self.homeTeamOrderNode,self.homeTeamNameNode];
    
    self.guestTeamNameNode.style.width = ASDimensionMake((LSScreenWidth -200)/2);
    self.guestTeamNameNode.maximumNumberOfLines = 2;
    self.guestTeamOrderNode.style.width = ASDimensionMake(LSAutoVlaue(25, LSScreenWidth));
    ASStackLayoutSpec * guestStackSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
    guestStackSpec.spacing = 3;
    guestStackSpec.justifyContent = ASStackLayoutJustifyContentStart;
    guestStackSpec.alignItems = ASStackLayoutAlignItemsCenter;
    guestStackSpec.children = @[self.guestTeamNameNode,self.guestTeamOrderNode];
    
    ASStackLayoutSpec * middleSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
    middleSpec.spacing = 5;
    middleSpec.justifyContent = ASStackLayoutJustifyContentStart;
    middleSpec.alignItems = ASStackLayoutAlignItemsCenter;
    middleSpec.children = @[homeStackSpec,self.vsNode,guestStackSpec];
    
    self.leagueNameNode.style.width = ASDimensionMake(LSAutoVlaue(50, LSScreenWidth));
    self.leagueNameNode.maximumNumberOfLines = 1;
    ASStackLayoutSpec * leagueSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
    leagueSpec.alignItems = ASStackLayoutAlignItemsCenter;
    leagueSpec.style.preferredSize = CGSizeMake(LSAutoVlaue(50, LSScreenWidth), LSAutoVlaue(40, LSScreenWidth));
    leagueSpec.children = @[self.leagueNameNode];
    
    
    ASStackLayoutSpec * topSpec = [ASStackLayoutSpec horizontalStackLayoutSpec];
    topSpec.justifyContent = ASStackLayoutJustifyContentSpaceBetween;
    topSpec.alignItems = ASStackLayoutAlignItemsCenter;
    topSpec.children = @[leagueSpec,middleSpec,self.timeNode];
    ASInsetLayoutSpec * insetSpec = [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(5, 10, 5, 10) child:topSpec];
    
    self.topViewNode.style.height = ASDimensionMake(50);
    self.topViewNode.backgroundColor = LSTitleWhiteColor;
    ASWrapperLayoutSpec * wraSpec = [ASWrapperLayoutSpec wrapperWithLayoutElement:self.topViewNode];
    ASCenterLayoutSpec * centerSpec = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:insetSpec];
    ASOverlayLayoutSpec *overSpec = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:wraSpec overlay:centerSpec];
  
    self.baseViewNode.style.height = ASDimensionMake(60);
//     ASWrapperLayoutSpec * baseWraSpec = [ASWrapperLayoutSpec wrapperWithLayoutElement:self.baseViewNode];
    ASCenterLayoutSpec * baseCenterSpec = [ASCenterLayoutSpec centerLayoutSpecWithCenteringOptions:ASCenterLayoutSpecCenteringXY sizingOptions:ASCenterLayoutSpecSizingOptionDefault child:overSpec];
    ASOverlayLayoutSpec * baseOverSpec = [ASOverlayLayoutSpec overlayLayoutSpecWithChild:self.baseViewNode overlay:baseCenterSpec];
    
    if (_model.isOpen) {
        self.tableNode.style.height = ASDimensionMake(185);
        self.anyMoreBtn.style.height = ASDimensionMake(30);
        DISPATCH_ASYNC_GET_MAIN(^{
            self.tableNode.view.scrollEnabled = NO;
            self.tableNode.view.separatorStyle = UITableViewCellSelectionStyleNone;
        });
    }else{
        
       self.anyMoreBtn.style.height = ASDimensionMake(.1);
    }

    
    ASStackLayoutSpec * allStack = [ASStackLayoutSpec stackLayoutSpecWithDirection:ASStackLayoutDirectionVertical spacing:0 justifyContent:ASStackLayoutJustifyContentStart alignItems:ASStackLayoutAlignItemsStretch children:@[baseOverSpec,self.tableNode,self.anyMoreBtn]];
    
   return allStack;
    
}
- (NSAttributedString *)attributedStringForDescription:(NSString *)text withFont:(UIFont *)font WithTextColor:(UIColor *)textColor{
    NSDictionary *descriptionAttributes = @{NSFontAttributeName: font,
                                            NSForegroundColorAttributeName: textColor,
                                            NSBackgroundColorAttributeName: [UIColor clearColor],
                                            NSParagraphStyleAttributeName: [self justifiedParagraphStyle]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:descriptionAttributes];
}

- (NSParagraphStyle *)justifiedParagraphStyle {
    NSMutableParagraphStyle *paragraphStlye = [[NSMutableParagraphStyle alloc] init];
    paragraphStlye.alignment = NSTextAlignmentCenter;
    return paragraphStlye;
}

#pragma mark - ASTableNodeDelegate & dataSource
- (NSInteger)tableNode:(ASTableNode *)tableNode numberOfRowsInSection:(NSInteger)section{
    if (typeIndex == 0) {
        return _model.OddsChangeRecordList.count;
    }else if (typeIndex == 1){
        return _model.AIHistoricaDataList.count;
    }
    return _model.SDHistoricaDataList.count;
}

- (ASCellNodeBlock)tableNode:(ASTableNode *)tableNode nodeBlockForRowAtIndexPath:(NSIndexPath *)indexPath{
    id model;
    if (typeIndex == 0) {
        model = _model.OddsChangeRecordList[indexPath.row];
    }else if (typeIndex == 1 ){
        model = _model.AIHistoricaDataList[indexPath.row];
    }else{
        model = _model.SDHistoricaDataList[indexPath.row];
    }
    return ^{
        LSExponentSubNode * node = [[LSExponentSubNode alloc] initWithType:typeIndex With:model];
        node.selectionStyle = UITableViewCellSelectionStyleNone;
        node.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        return node;
    };
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 74;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 74)];
//    header.backgroundColor = self.bottomView.backgroundColor;

    LSTypeButton  *typeBtnView = [[LSTypeButton  alloc]initWithFrame:CGRectMake(20, 0, header.frame.size.width-40, 37)];
    [typeBtnView setTypeButtonTitles:@[@"欧赔",@"亚盘",@"大小球"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setSelectButtonIndex:typeIndex];
    [typeBtnView setTypeButtonNormalColor:LSTitleGrayColor4 andSelectColor:LSNavMainColor];
    typeBtnView.backgroundColor = header.backgroundColor;
    [header addSubview:typeBtnView];

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(10, 37, header.frame.size.width-20, 37)];
    titleView.backgroundColor = LSTitleWhiteColor;
    [header addSubview:titleView];
    NSArray *titleArray;
    switch (typeIndex) {
        case 0:
            titleArray = @[@"公司",@"初盘/即时",@"胜",@"平",@"负"];
            break;
        case 1:{
            titleArray = @[@"公司",@"初盘/即时",@"主队",@"盘口",@"客队"];
        }
            break;
        case 2:{
            titleArray = @[@"公司",@"初盘/即时",@"大球",@"盘口",@"小球"];
        }
            break;
        default:
            break;
    }
    //    NSArray *titleArray = @[@"公司",@"即时/初盘",@"主队",@"让球",@"客队"];
    CGFloat width = titleView.frame.size.width/5;
    UILabel *lable;
    for (int i=0; i<5; i++) {
        lable=[UILabel new];
        lable.text = titleArray[i];
        lable.textColor = LSSubColor5D5D5D;
        lable.font=LSFontSize12;
        lable.textAlignment = NSTextAlignmentCenter;
        [titleView addSubview:lable];
        lable.sd_layout.leftEqualToView(titleView).offset(width*i).centerYEqualToView(titleView).widthIs(width).heightIs(37);
    }
    return header;

}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index{
    NSLog(@"现在点击的是%zd个",index);
    typeIndex = index;
    
    [self.tableNode reloadData];
}

- (void)anyMoreBtnClicked:(ASButtonNode *)node{
    if (self.ClickCheckMoreBtnBlock) {
        self.ClickCheckMoreBtnBlock();
    }
}
@end
